#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
详细测试情绪识别准确率
"""

import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from core.utils.emotionAnalyzer import emotion_analyzer

def detailed_test():
    """详细测试各种情绪识别"""
    
    test_cases = [
        # Happy 开心 - 应该识别为happy
        ("今天天气真好，我很开心！", "happy"),
        ("太棒了，这个结果我很满意！", "happy"),
        ("哈哈，这个笑话真好笑！", "happy"),
        ("我很快乐", "happy"),
        ("心情很好", "happy"),
        
        # Sad 难过 - 应该识别为sad
        ("今天心情不好，有点难过", "sad"),
        ("失去了重要的东西，很伤心", "sad"),
        ("这次考试没考好，很失望", "sad"),
        ("我很沮丧", "sad"),
        ("心情低落", "sad"),
        
        # Angry 愤怒 - 应该识别为angry
        ("这件事让我很生气！", "angry"),
        ("太讨厌了，气死我了", "angry"),
        ("这个结果太糟糕了！", "angry"),
        ("我很愤怒", "angry"),
        ("气死我了", "angry"),
        
        # Fear 恐惧 - 应该识别为fear
        ("我害怕一个人走夜路", "fear"),
        ("这个恐怖片太吓人了", "fear"),
        ("我很担心明天的面试", "fear"),
        ("我很害怕", "fear"),
        ("感到恐惧", "fear"),
        
        # Surprise 惊讶 - 应该识别为surprise
        ("哇，这个结果太意外了！", "surprise"),
        ("没想到会是这样", "surprise"),
        ("太震惊了，居然是这样", "surprise"),
        ("我很惊讶", "surprise"),
        ("太意外了", "surprise"),
        
        # Disgust 厌恶 - 应该识别为disgust
        ("这个味道太恶心了", "disgust"),
        ("我讨厌这种食物", "disgust"),
        ("这个行为让我很反感", "disgust"),
        ("太恶心了", "disgust"),
        ("很厌恶", "disgust"),
        
        # Neutral 中性 - 应该识别为neutral
        ("嗯，我知道了", "neutral"),
        ("好的，明白了", "neutral"),
        ("一般般吧", "neutral"),
        ("还可以", "neutral"),
        ("无所谓", "neutral"),
    ]
    
    print("详细测试情绪识别准确率")
    print("=" * 60)
    
    emotion_stats = {}
    correct_count = 0
    total_count = len(test_cases)
    
    for text, expected_emotion in test_cases:
        # 测试综合方法
        result = emotion_analyzer.analyze_emotion_combined(text)
        predicted_emotion = result.get("emotion", "unknown")
        confidence = result.get("confidence", 0)
        
        # 测试SnowNLP方法
        snownlp_result = emotion_analyzer.analyze_emotion_snownlp(text)
        snownlp_emotion = snownlp_result.get("emotion", "unknown")
        snownlp_score = snownlp_result.get("sentiment_score", 0)
        
        # 测试关键词方法
        keyword_result = emotion_analyzer.analyze_emotion_keywords(text)
        keyword_emotion = keyword_result.get("emotion", "unknown")
        
        # 检查预测是否正确
        is_correct = predicted_emotion == expected_emotion
        if is_correct:
            correct_count += 1
        
        # 统计各情绪的表现
        if expected_emotion not in emotion_stats:
            emotion_stats[expected_emotion] = {"correct": 0, "total": 0}
        emotion_stats[expected_emotion]["total"] += 1
        if is_correct:
            emotion_stats[expected_emotion]["correct"] += 1
        
        status = "OK" if is_correct else "FAIL"
        print(f"{status} 文本: {text}")
        print(f"   预期: {expected_emotion}")
        print(f"   综合: {predicted_emotion} (置信度: {confidence:.3f})")
        print(f"   SnowNLP: {snownlp_emotion} (得分: {snownlp_score:.3f})")
        print(f"   关键词: {keyword_emotion}")
        print()
    
    accuracy = correct_count / total_count * 100
    print("=" * 60)
    print(f"总体准确率: {correct_count}/{total_count} ({accuracy:.1f}%)")
    print()
    
    # 各情绪准确率统计
    print("各情绪准确率统计:")
    print("-" * 30)
    for emotion, stats in emotion_stats.items():
        emotion_accuracy = stats["correct"] / stats["total"] * 100
        print(f"{emotion}: {stats['correct']}/{stats['total']} ({emotion_accuracy:.1f}%)")
    
    return accuracy, emotion_stats

if __name__ == "__main__":
    try:
        accuracy, stats = detailed_test()
        print(f"\n测试完成！总体准确率: {accuracy:.1f}%")
        
    except Exception as e:
        print(f"测试失败: {e}")
        import traceback
        traceback.print_exc()
